package me.minetsh.imaging.view.stick;

import ohos.agp.components.Component;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Created by felix on 2017/11/17 下午6:08.
 */
public class IMGStickerMoveHelper {
    private Component mView;
    private Point pointerCurrentPoint = new Point();
    private Point pointerStartingPoint = new Point();
    private float startTransX = 0f;
    private float startTransY = 0f;

    /**
     * 构造函数
     *
     * @param view 视图
     */
    public IMGStickerMoveHelper(Component view) {
        mView = view;
    }

    /**
     * touch事件
     *
     * @param view  视图
     * @param event 事件
     * @return 是否拦截
     */
    public boolean onTouch(Component view, TouchEvent event) {
        float dx;
        float dy;
        switch (event.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                pointerStartingPoint.modify(event.getPointerScreenPosition(0).getX(),
                        event.getPointerScreenPosition(0).getY());
                mView.setTranslation(mView.getTranslationX(), mView.getTranslationY());
                startTransX = mView.getTranslationX();
                startTransY = mView.getTranslationY();
                return true;
            case TouchEvent.POINT_MOVE:
                pointerCurrentPoint.modify(event.getPointerScreenPosition(0).getX(),
                        event.getPointerScreenPosition(0).getY());

                dy = pointerCurrentPoint.getPointY() - pointerStartingPoint.getPointY();
                dx = pointerCurrentPoint.getPointX() - pointerStartingPoint.getPointX();

                mView.setTranslation(startTransX + dx, startTransY + dy);
                return true;
        }
        return false;
    }
}
